package com.monkeys.service.impl;

import com.monkeys.mapper.PointsMapper;
import com.monkeys.pojo.GoodsDemand;
import com.monkeys.pojo.Points;
import com.monkeys.service.PoiService;
import com.monkeys.service.PointsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
 * @author KuangTeam
 */
@Service
@RequiredArgsConstructor
public class PointsServiceImpl implements PointsService {

    private final PointsMapper pointsMapper;
    private final PoiService poiService;


    //根据订单信息生成节点信息
    @Override
    public List<Points> createPoints(List<GoodsDemand> goodsDemandList) {
        List<Points> pointsList = new ArrayList<>();

        //一个订单生成两个节点
        for (GoodsDemand goodsDemand : goodsDemandList) {
            //发送方节点
            Points point1 = Points.builder().goodsDemandId(goodsDemand.getId())
                    .targetId(goodsDemand.getSenderId())
                    .weight(goodsDemand.getWeight())
                    .deadTime(goodsDemand.getDeadTime())
                    .lat(poiService.getById(goodsDemand.getSenderId()).getLat())
                    .lng(poiService.getById(goodsDemand.getSenderId()).getLng())
                    .build();
            //目的地节点
            Points point2 = Points.builder().goodsDemandId(goodsDemand.getId())
                    .targetId(goodsDemand.getOwnerId())
                    .weight(goodsDemand.getWeight())
                    .deadTime(goodsDemand.getDeadTime())
                    .lat(poiService.getById(goodsDemand.getOwnerId()).getLat())
                    .lng(poiService.getById(goodsDemand.getOwnerId()).getLng())
                    .build();

            //插入数据库
            pointsMapper.insert(point1);
            pointsMapper.insert(point2);
            //插入集合
            pointsList.add(point1);
            pointsList.add(point2);
        }

        return pointsList;
    }


    //删除全部信息
    @Override
    public void deleteAll() {
        pointsMapper.deleteAll();
    }
}
